#!/usr/bin/env python3
# encoding: utf-8
"""
@author: kevin
@contact: evil365@gmail.com
@project : flask
@file: census.py
@time: 2020/8/11 13:14
@license: (C) Copyright 2019-2025, Corporation Limited.
@desc:
"""


from app.models.zf_model import client, clientposion, clientrepment, clientcashdetail, product, productinfo, t_sens_repayment_view as tview
from datetime import date, datetime, timedelta
from sqlalchemy import extract, func, desc
from decimal import Decimal

today = date.today()
last_day = today + timedelta(7)


class census(clientrepment):

    @classmethod
    def get_census(cls):
        total_quota = productinfo.query.with_entities(func.sum(productinfo.quota)).filter_by(delete_time=None).scalar()
        total_yield = cls.query.with_entities(func.sum(cls.yieldBlance)).filter_by(status='已还款', delete_time=None).scalar()
        total_user = client.query.filter_by(delete_time=None).count()
        total_sentment = cls.query.with_entities(func.sum(cls.capitalBlance)+func.sum(cls.yieldBlance)).filter(cls.status=='已还款', cls.delete_time==None).scalar()
        if total_quota is None:
            total_quota = 0.00
        if total_yield is None:
            total_yield = 0.00
        if total_sentment is None:
            total_sentment = 0.00
        census_list = {'user': total_user, 'quota': float(total_quota.quantize(Decimal('0.00'))), 'sentment': float(total_sentment.quantize(Decimal('0.00'))), 'yield': float(total_yield.quantize(Decimal('0.00')))}
        return census_list

    @classmethod
    def get_today_data(cls):
        user = client.query.filter(extract('year', client.registTime) == date.today().year,
                                      extract('month', client.registTime) == date.today().month,
                                      extract('day', client.registTime) == date.today().day,
                                      cls.delete_time == None).count()
        quota = cls.query.with_entities(func.sum(cls.capitalBlance)).filter(extract('year', cls.create_time) == date.today().year,
                                                 extract('month', cls.create_time) == date.today().month,
                                                 extract('day', cls.create_time) == date.today().day,
                                                 cls.delete_time == None).group_by(cls.clientPosionSn).scalar()
        setment = cls.query.with_entities(func.sum(cls.capitalBlance)+func.sum(cls.yieldBlance)).filter(extract('year', cls.update_time) == date.today().year,
                                                                  extract('month', cls.update_time) == date.today().month,
                                                                  extract('day', cls.update_time) == date.today().day,
                                                                  cls.delete_time == None,
                                                                  cls.type != '转让').group_by(cls.clientPosionSn).scalar()
        if quota:
            if setment:
                today_list = {'user': user, 'quota': float(quota.quantize(Decimal('0.00'))), 'settment': float(setment.quantize(Decimal('0.00')))}
            else:
                today_list = {'user': user, 'quota': float(quota.quantize(Decimal('0.00'))), 'settment': float(0)}
        else:
            today_list = {'user': user, 'quota': float(0), 'settment': float(0)}
        return today_list

    @classmethod
    def get_cens_posion_chart(cls):
        result = []
        quota = productinfo.query.with_entities(func.sum(productinfo.quota), productinfo.valueStartDate).group_by(productinfo.valueStartDate).all()
        repay = cls.query.with_entities(tview.columns['capital'], tview.columns['date_time']).all()
        ryield = cls.query.with_entities(tview.columns['yield'], tview.columns['date_time']).all()
        for i in quota:
            quota_list = {'value': int(i[0].quantize(Decimal('0.00'))), 'date': i[1], 'type': '募集额'}
            result.append(quota_list)
        for i in repay:
            # print(i)
            captial_list = {'value': int(i[0].quantize(Decimal('0.00'))), 'date': i[1], 'type': '清算本金'}
            result.append(captial_list)
        for i in ryield:
            yield_list = {'value': int(i[0].quantize(Decimal('0.00'))), 'date': i[1], 'type': '清算收益'}
            result.append(yield_list)
        # print(result)
        return result


class cen_pro(product):

    @classmethod
    def get_last_product(cls):
        new = cls.query.with_entities(cls.name, productinfo.valueStartDate).outerjoin(productinfo, cls.sn == productinfo.productSn).filter(cls.status == '已成立').order_by(cls.sn.desc()).limit(5).all()
        sem = cls.query.with_entities(cls.name, productinfo.settmentDate).outerjoin(productinfo, cls.sn == productinfo.productSn).filter(cls.status == '已成立').filter(productinfo.settmentDate.between(today, last_day)).all()
        # print(sem)
        result = {'new': new, 'setment': sem}
        return result
